const app = getApp()
Page({

  /**  
   * 页面的初始数据
   */
  data: {
    activeNames: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    activeNames2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    receivable: "5",
    img: "",
    list: [],
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { audit = false, stockOrderId = 0, auxiliaryMaterialsId = 0, smallCanteenPurchaseId = 0,  collarId = 0, type = 0 } = JSON.parse(options.data)

    this.setData({ audit, stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, collarId, type },()=> {
      this.getDetails()
    })
  },
  getDetails(){
    const { type, stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, collarId} =this.data
    console.log(type)
    if (type === 0) {
      wx.setNavigationBarTitle({
        title: '食材进货清单',
      })
      app.reqFetch.getOrderDetails({ stockOrderId }, (res) => {
        const { stockorder, list } = res
        this.setData({ stockorder, list })
      })
    } else if (type === 1) {
      wx.setNavigationBarTitle({
        title: '主食进货清单',
      })
      app.reqFetch.auxiliarymaterialsmxDetails({ auxiliaryMaterialsId }, (res) => {
        const list = res.list
        this.setData({ list })
      })
    } else if (type === 2) {
      wx.setNavigationBarTitle({
        title: '食堂申请清单',
      })
      app.reqFetch.smallcanteenpurchasemxdetails({ smallCanteenPurchaseId }, (res) => {
        const canteensList = JSON.parse(res.smallCanteenPurchase.boxId).list
        this.setData({
          canteensList
        })
        const list = res.list
        this.setData({ list })
      })
    } else if (type === 3) {
      wx.setNavigationBarTitle({
        title: '主食领取清单',
      })
      app.reqFetch.collarmxdetails({ collarId }, (res) => {
        const { collarMx } = res
        const list = [{ gys: '主食领取列表', mxlist: collarMx }]
        this.setData({ list })
      })
    }
  },
  chooseWxImage: function (record) {
    var that = this;
    wx.chooseImage({
      sizeType: 'compressed',
      count:1,
      success: res => {
        wx.uploadFile({
          url: 'https://api.canteen.jiangshan.hzzhixun.cn/canteen-api/canteens/oss/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: (res) => {
            const imgUrl = JSON.parse(res.data).data.src
            let { list, type } = that.data
            let { supplierId, foodId, stockOrdermxId, auxiliaryMaterialsmxId, smallCanteenPurchasemxId, collarmxId } = record

            if (type === 0) {
              app.reqFetch.TakePhoto({
                entityPhoto: imgUrl,
                status: "3",
                stockOrdermxId: stockOrdermxId
              }, (res) => {
                console.log(res)
                if (res.code!==0){
                  wx.showToast({
                    title: res.msg,
                    icon: 'none'
                  })
                }
                this.getDetails()
              })
            } else if (type === 1) {
              app.reqFetch.updatePhoto({
                entityPhoto: imgUrl,
                status: "3",
                auxiliaryMaterialsmxId
              }, (res) => {
                if (res.data.code !== 0) {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                }
                this.getDetails()
              })
            } else if (type === 2) {
              app.reqFetch.smallcaupdatePhoto({
                entityPhoto: imgUrl,
                status: "3",
                smallCanteenPurchasemxId
              }, (res) => {
                if (res.data.code !== 0) {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                }
                this.getDetails()
              })
            } else if (type === 3) {
              app.reqFetch.collarmxupdatePhoto({
                entityPhoto: imgUrl,
                status: "3",
                collarmxId
              }, (res) => {
            
                this.getDetails()
              })
            }
          }
        })

      }
    })
  },
  chooseimage: function (e) {
    if (!this.data.audit) {
      console.log(e)
      const { entityPhoto } = e.currentTarget.dataset.record
      this.clickImg(entityPhoto)
      return
    }
    const record = e.currentTarget.dataset.record
    this.chooseWxImage(record)
    return
    wx.showActionSheet({
      itemList: ['拍照','选择照片'],
      itemColor: "#333333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
          
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera', record)
          }
        }
      }
    })
  },
  clickImg: function (entityPhoto) {

    wx.previewImage({
      urls: [entityPhoto], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeCollapse(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  arrivalNum: function (e) {
    const { type } = this.data
    const { stockOrdermxId, auxiliaryMaterialsmxId, smallCanteenPurchasemxId, collarmxId } = e.target.dataset.record

    if (type === 0) {
      app.reqFetch.TakePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        stockOrdermxId: stockOrdermxId
      }, (res) => {
        if (res.code !== 0) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
        this.getDetails()
      })
    } else if (type === 1) {
      app.reqFetch.updatePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        auxiliaryMaterialsmxId
      }, (res) => {
        if (res.data.code !== 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        this.getDetails()
      })
    } else if (type === 2) {
      app.reqFetch.smallcaupdatePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        smallCanteenPurchasemxId
      }, (res) => {
        if (res.data.code !== 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        this.getDetails()
      })
    } else if (type === 3) {
     console.log(e)
      const { purchaseNum} = e.currentTarget.dataset.record
        if(e.detail.value > purchaseNum){
          wx.showToast({
            title: '超出食材上限',
            icon: 'none'
          })
            return
        }
      app.reqFetch.collarmxupdatePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        collarmxId
      }, (res) => {
 
        this.getDetails()
      })
    }



  },

  handelConfirm: function (e) {
    const { stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, collarId, type, list } = this.data
    var flag =false
    list.map(item=>{
      item.mxlist.map(ele=>{
        if (!ele.entityPhoto ){
          flag=true
          }
      })
    })
    if (flag){
      wx.showToast({
        title: '还未全部验收完成，请继续进行验收',
        icon: 'none'
      })
      return
    }

    if (type === 0) {
      app.reqFetch.pageAdminUpdate({ stockOrderId, status: 3 }, (res) => {
        console.log(res)
        if (res.code === 0) {
          wx.showToast({
            title: '食材进货清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else if (type === 1) {
      app.reqFetch.pageAdminUpdate2({ auxiliaryMaterialsId, status: 3 }, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '主食进货清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
    } else if (type === 2) {
      app.reqFetch.smallcanteenpurchasepageAdminUpdate({ smallCanteenPurchaseId, status: 3 }, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '小食堂申请清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else if (type === 3) {
      app.reqFetch.collarpageAdminUpdate({ collarId, status: 3 }, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '主食领取清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    }
  },
  handleCancel() {
    wx.navigateBack({
      delta: 1,
    })
  }
})